package cn.lg.org.controller;

import cn.lg.basic.util.AjaxResult;
import cn.lg.basic.util.PageList;
import cn.lg.org.domain.Department;
import cn.lg.org.query.DepartmentQuery;
import cn.lg.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
* @Description: 部门：controller接口
* @Author: lg
* @Version: 1.0
* @Date:  2022/4/19 19:18
*/
@RestController
@RequestMapping("/dept")
//接口文档的注解
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    /**
     *  ctrl + alt + t  try，if等方法的快捷键
     * 1.查询全部 - @GetMapping
     * 2.单个查询 - @GetMapping("/id")
     * 3.删除 - @DeleteMapping("/id")
     * 4.修改or添加 - @PutMapping
     * 5.批量删除 - @PatchMapping("/batch")
     * 6.查询无限极部门树 - @GetMapping("/tree")
     */
    @Autowired
    private IDepartmentService departmentService;

   /**
   * @Title:
   * @Description: 部门：查询所有
   * @Author: lg
   * @Version: 1.0
   * @Date:  2022/4/19 19:18
   * @Parameters: * @param null
   * @Return
   */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

  /**
  * @Title:
  * @Description: 部门：单个查询
  * @Author: lg
  * @Version: 1.0
  * @Date:  2022/4/19 19:19
  * @Parameters: * @param null
  * @Return
  */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryById(@ApiParam(name="编号",required = true) @PathVariable("id") Long id){
        return departmentService.queryById(id);
    }


    /**
    * @Title:
    * @Description: 分页查询
    * @Author: lg
    * @Version: 1.0
    * @Date:  2022/4/19 19:21
    * @Parameters: * @param null
    * @Return
    */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryData(departmentQuery);
    }

    /**
    * @Title:
    * @Description: 部门：删除
    * @Author: lg
    * @Version: 1.0
    * @Date:  2022/4/19 19:22
    * @Parameters: * @param null
    * @Return
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult remove(@ApiParam(name="编号",required = true) @PathVariable("id")Long id) {
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * @Title:
    * @Description: 部门：添加或修改
    * @Author: lg
    * @Version: 1.0
    * @Date:  2022/4/19 19:23
    * @Parameters: * @param null
    * @Return
    */
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() != null){
                departmentService.update(department);
            }else{
                departmentService.save(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * @Title:
    * @Description: 部门：批量删除
    * @Author: lg
    * @Version: 1.0
    * @Date:  2022/4/19 19:23
    * @Parameters: * @param null
    * @Return
    */
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchRomove(@RequestBody List<Long> ids) {
        try {
            departmentService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * @Title:
    * @Description: 部门：无限极部门树
    * @Author: lg
    * @Version: 1.0
    * @Date:  2022/4/19 19:24
    * @Parameters: * @param null
    * @Return
    */
    @GetMapping("/tree")
    @ApiOperation(value = "无极限树" )
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
